﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Library;

namespace Sport_Shop
{
    public partial class ChiTietSP : System.Web.UI.Page
    {
        static DataTable tbGioHang = new DataTable();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["GioHang"] != null)
                {
                    tbGioHang = Session["GioHang"] as DataTable;
                }
                else
                {
                    tbGioHang.Rows.Clear();
                    tbGioHang.Columns.Clear();
                    tbGioHang.Columns.Add("ID", typeof(int));
                    tbGioHang.Columns.Add("TenSP", typeof(string));
                    tbGioHang.Columns.Add("DonGia", typeof(int));
                    tbGioHang.Columns.Add("SoLuong", typeof(int));
                    tbGioHang.Columns.Add("ThanhTien", typeof(int), "SoLuong * DonGia");
                }
                load_CTSP();
            }

        }

        private void load_CTSP()
        {
            string masp = Request.QueryString["MaSP"].ToString();

            List<Library.SanPham> result = new List<Library.SanPham>();
            result = Library.SanPham.GetSPByMa(masp);

            dlChiTietSP.DataSource = result;
            dlChiTietSP.DataBind();
        }
        protected void Back_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Index.aspx");
        }



        protected void dlChiTietSP_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "DatHang")
            {
                int idsp = Convert.ToInt32(dlChiTietSP.DataKeys[e.Item.ItemIndex].ToString());
                string tensp = ((Label)e.Item.FindControl("lbTenSP")).Text;
                int gia = int.Parse(((Label)e.Item.FindControl("lbGia")).Text);
                int soluong = 1;

                //Add vao gio hang

                foreach (DataRow row in tbGioHang.Rows)
                {//Kiem tra neu mat hang da co roi thi tang so luong len 1
                    if (row["TenSP"].ToString() == tensp)
                    {
                        row["SoLuong"] = (int)row["SoLuong"] + 1;
                        goto GioHang;
                    }

                }
                tbGioHang.Rows.Add(idsp, tensp, gia, soluong);

                
            GioHang:
                Session["GioHang"] = tbGioHang;
                Response.Write("<script>alert('Đã thêm vào giỏ hàng !!!')</script>");
            //Response.Redirect("~/Giohang?TenSP=" + tensp);

            }

        }

     
        //protected void DatHang_Click(object sender, EventArgs e)
        //{
        //    string tensp = dlChiTietSP.FindControl("lbTenSP").ToString();
        //    int gia = int.Parse(dlChiTietSP.FindControl("lbGia").ToString());
        //    int soluong = 1;

        //    foreach (DataRow row in tbGioHang.Rows)
        //    {
        //        if (row["TenSP"] == tensp)
        //        {
        //            row["SoLuong"] = (int)row["SoLuong"] + 1;
        //            goto GioHang;
        //        }

        //    }
        //    tbGioHang.Rows.Add(tensp, gia, soluong);
        //GioHang:
        //    Session["GioHang"] = tbGioHang;
        //    //Response.Redirect("~/Giohang.aspx?TenSP=" + tensp);
        //}
    }
}

